package com.harmontronics.hanma.paas.paas.controller;

import com.harmontronics.hanma.paas.paas.domain.iam.entity.UserGroupData;
import com.harmontronics.hanma.paas.paas.token.AppUserTokenKit;
import com.harmontronics.hanma.paas.paas.utils.RequestUtils;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmUserGroupDataAppService;
import com.harmontronics.hanma.paas.paas.dto.iam.output.HmUserGroupDataResult;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmUserGroupDataCreateInput;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmUserGroupData;


/**
 * ${comments}
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-07 10:35:20
 */
@RestController
@RequestMapping("/api/hmusergroupdata")
@Api(tags = {"[权限]用户组数据权限管理"})
public class HmUserGroupDataController {
    @Autowired
    private HmUserGroupDataAppService hmUserGroupDataappService;

    @Autowired
    private AppUserTokenKit appUserTokenKit;

    /**
     * 列表
     */
    @GetMapping("/listByGroupId")
    @ApiOperation("查询数据权限")
    public ResponseEntity<List<HmUserGroupDataResult>> list(@RequestParam Long groupId) {
        List<HmUserGroupData> hmUserGroupDataList = hmUserGroupDataappService.listByGroupId(groupId);
        return ResponseEntity.ok(Convert.toList(HmUserGroupDataResult.class, hmUserGroupDataList));
    }

    @GetMapping("/getTypeByScopeAndGroupId")
    @ApiOperation(value = "查询数据权限")
    public ResponseEntity getTypeByScopeAndGroupId(@RequestParam String scope, @RequestParam Long groupId) {
        return ResponseEntity.ok(hmUserGroupDataappService.getTypeByScopeAndGroupId(scope, groupId));
    }

    @GetMapping("getUserVal")
    @ApiOperation("获取用户已授权信息")
    public ResponseEntity getUserVal(@RequestParam String scope,@RequestParam String component,@RequestParam Long appId){
        Long userId = appUserTokenKit.getId();
        List<String> vals = hmUserGroupDataappService.getUserVals(scope,component,userId,appId);
        return ResponseEntity.ok(vals);
    }


    @PostMapping
    @ApiOperation(value = "创建数据权限")
    public ResponseEntity create(@RequestBody HmUserGroupDataCreateInput groupDataCreateDto) {
        UserGroupData UserGroupData =BeanUtil.toBean(groupDataCreateDto, UserGroupData.class);
        hmUserGroupDataappService.create(UserGroupData);
        return ResponseEntity.ok();
    }


    @DeleteMapping
    @ApiOperation(value = "删除数据权限")
    public ResponseEntity delete(@RequestParam String scope,@RequestParam Integer dataTypeId,@RequestParam Integer userGroupId) {
        hmUserGroupDataappService.remove(scope,dataTypeId,userGroupId);
        return ResponseEntity.ok();
    }


    @DeleteMapping("remove-val")
    @ApiOperation(value = "删除数据权限")
    public ResponseEntity deleteVal(@RequestParam String scope,@RequestParam Integer dataTypeId,@RequestParam Integer userGroupId,@RequestParam String val) {
        hmUserGroupDataappService.remove(scope,dataTypeId,userGroupId,val);
        return ResponseEntity.ok();
    }

}
